package cn.net.isir.controller.miniprogram;

import cn.net.isir.common.CacheObjectManager;
import cn.net.isir.common.RequestObject;
import cn.net.isir.common.ResultObject;
import cn.net.isir.entity.Turn;
import cn.net.isir.service.CRMUserManagerService;
import cn.net.isir.service.FactorService;
import cn.net.isir.service.MedalsService;
import cn.net.isir.service.TurnService;
import cn.net.isir.service2.MyCommissionService;
import cn.net.isir.utils.AppRequestUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename MyCommission.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2019/03/20
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/minip/commission")
public class MyCommissionController {
    @Resource
    private CRMUserManagerService crmUserManagerService;
    @Resource
    private MyCommissionService myCommissionService;
    @Resource
    private FactorService factorService;
    @Resource
    private MedalsService medalsService;
    @Resource
    private TurnService turnService;

    /**
     * 获取首页数据
     *
     * @param obj 参数
     * @return 首页数据
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ResultObject getIndexData(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        Map<String, BigDecimal> data = myCommissionService.getIndexData(userId,
                crmUserManagerService.getTurnedShopIds(),
                crmUserManagerService.getTimeRangeList(userId),
                factorService.selectOne(null),
                medalsService.selectList(null)
        );
        return new ResultObject(200, "success", data);
    }


    /**
     * 获取本月不可结转店铺列表
     *
     * @return 列表
     */
    @RequestMapping(value = "/cannotshoplist", method = RequestMethod.GET)
    public ResultObject getCannotTurnShopList(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }

        List<Map<String, Object>> data = myCommissionService.getCannotTurnShopList(userId,
                crmUserManagerService.getTurnedShopIds(),
                factorService.selectOne(null),
                crmUserManagerService.getTimeRangeList(userId));
        return new ResultObject(200, "success", data);
    }


    /**
     * 获取本月可结转店铺列表
     *
     * @return 本月可结转店铺列表
     */
    @RequestMapping(value = "/canturnshoplist", method = RequestMethod.GET)
    public ResultObject getCanTurnShopList(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }

        List<Map<String, Object>> data = myCommissionService.getCanTurnShopList(userId,
                crmUserManagerService.getTurnedShopIds(),
                factorService.selectOne(null),
                crmUserManagerService.getTimeRangeList(userId));
        return new ResultObject(200, "success", data);
    }

    /**
     * 获取本月可结转店铺列表
     *
     * @return 本月可结转店铺列表
     */
    @RequestMapping(value = "/history", method = RequestMethod.GET)
    public ResultObject history(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }

        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        Turn turn = new Turn();
        turn.setUserId(userId);
        List<Turn> turnList = turnService.selectList(turn);
        for(Turn t : turnList){
            if (new BigDecimal("0.000000").compareTo(t.getTurnFee()) == 0 || new BigDecimal("0.000000").compareTo(t.getTurnFee()) == 1){
                return new ResultObject(200, "success", new ArrayList<>());
            }
            map.put("monthFlow",t.getMonthFee());
            map.put("monthTurn",t.getTurnFee());
            map.put("turnTime",t.getTurnTime());
        }
        data.add(map);
        return new ResultObject(200, "success", data);
    }
}
